{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "from pandas import Series, DataFrame, Index, MultiIndex\n",
    "#from pandas_datareader import data as web"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取index\n",
      "Index(['b', 'c'], dtype='object')\n",
      "<class 'TypeError'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('获取index')\n",
    "obj = Series(range(3), index = ['a', 'b', 'c'])\n",
    "index = obj.index\n",
    "print(index[1:])\n",
    "try:\n",
    "    index[1] = 'd'  # index对象read only\n",
    "except:\n",
    "    print(sys.exc_info()[0])\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用Index对象\n",
      "0    1.5\n",
      "1   -2.5\n",
      "2    0.0\n",
      "dtype: float64\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print ('使用Index对象')\n",
    "index = Index(np.arange(3))\n",
    "obj2 = Series([1.5, -2.5, 0], index = index)\n",
    "print(obj2)\n",
    "print(obj2.index is index)\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "判断列和索引是否存在\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('判断列和索引是否存在')\n",
    "pop = {'Nevada':{20001:2.4, 2002:2.9},\n",
    "        'Ohio':{2000:1.5, 2001:1.7, 2002:3.6}}\n",
    "frame3 = DataFrame(pop)\n",
    "print('Ohio' in frame3.columns)\n",
    "print('2003' in frame3.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新指定索引及顺序\n",
      "d    4.5\n",
      "b    7.2\n",
      "a   -5.3\n",
      "c    3.6\n",
      "dtype: float64\n",
      "a   -5.3\n",
      "b    7.2\n",
      "d    4.5\n",
      "c    3.6\n",
      "e    NaN\n",
      "dtype: float64\n",
      "a   -5.3\n",
      "b    7.2\n",
      "d    4.5\n",
      "c    3.6\n",
      "e    0.0\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('重新指定索引及顺序')\n",
    "obj = Series([4.5, 7.2, -5.3, 3.6], index = ['d', 'b', 'a', 'c'])\n",
    "print(obj)\n",
    "obj2 = obj.reindex(['a', 'b', 'd', 'c', 'e'])\n",
    "print(obj2)\n",
    "print(obj.reindex(['a', 'b', 'd', 'c', 'e'], fill_value = 0))  # 指定不存在元素的默认值\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新指定索引并指定填元素充方法\n",
      "0      blue\n",
      "2    purple\n",
      "4    yellow\n",
      "dtype: object\n",
      "0      blue\n",
      "1      blue\n",
      "2    purple\n",
      "3    purple\n",
      "4    yellow\n",
      "5    yellow\n",
      "dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('重新指定索引并指定填元素充方法')\n",
    "obj3 = Series(['blue', 'purple', 'yellow'], index = [0, 2, 4])\n",
    "print(obj3)\n",
    "print(obj3.reindex(range(6), method = 'ffill'))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对DataFrame重新指定索引\n",
      "   Ohio  Texas  California\n",
      "a     0      1           2\n",
      "c     3      4           5\n",
      "d     6      7           8\n",
      "   Ohio  Texas  California\n",
      "a   0.0    1.0         2.0\n",
      "b   NaN    NaN         NaN\n",
      "c   3.0    4.0         5.0\n",
      "d   6.0    7.0         8.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('对DataFrame重新指定索引')\n",
    "frame = DataFrame(np.arange(9).reshape(3, 3),\n",
    "                  index = ['a', 'c', 'd'],\n",
    "                  columns = ['Ohio', 'Texas', 'California'])\n",
    "print(frame)\n",
    "frame2 = frame.reindex(['a', 'b', 'c', 'd'])\n",
    "print(frame2)\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新指定column\n",
      "   Texas  Utah  California\n",
      "a      1   NaN           2\n",
      "c      4   NaN           5\n",
      "d      7   NaN           8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('重新指定column')\n",
    "states = ['Texas', 'Utah', 'California']\n",
    "print(frame.reindex(columns = states))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series根据索引删除元素\n",
      "a    0.0\n",
      "b    1.0\n",
      "d    3.0\n",
      "e    4.0\n",
      "dtype: float64\n",
      "a    0.0\n",
      "b    1.0\n",
      "e    4.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('Series根据索引删除元素')\n",
    "obj = Series(np.arange(5.), index = ['a', 'b', 'c', 'd', 'e'])\n",
    "new_obj = obj.drop('c')\n",
    "print(new_obj)\n",
    "print(obj.drop(['d', 'c']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame删除元素，可指定索引或列。\n",
      "          one  two  three  four\n",
      "Ohio        0    1      2     3\n",
      "Colorado    4    5      6     7\n",
      "Utah        8    9     10    11\n",
      "New York   12   13     14    15\n",
      "          one  two  three  four\n",
      "Utah        8    9     10    11\n",
      "New York   12   13     14    15\n",
      "          one  three  four\n",
      "Ohio        0      2     3\n",
      "Colorado    4      6     7\n",
      "Utah        8     10    11\n",
      "New York   12     14    15\n",
      "          one  three\n",
      "Ohio        0      2\n",
      "Colorado    4      6\n",
      "Utah        8     10\n",
      "New York   12     14\n"
     ]
    }
   ],
   "source": [
    "print('DataFrame删除元素，可指定索引或列。')\n",
    "data = DataFrame(np.arange(16).reshape((4, 4)),\n",
    "                  index = ['Ohio', 'Colorado', 'Utah', 'New York'],\n",
    "                  columns = ['one', 'two', 'three', 'four'])\n",
    "print(data)\n",
    "print(data.drop(['Colorado', 'Ohio']))\n",
    "print(data.drop('two', axis = 1))\n",
    "print(data.drop(['two', 'four'], axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series的索引，默认数字索引可以工作。\n",
      "1.0\n",
      "3.0\n",
      "b    1.0\n",
      "d    3.0\n",
      "dtype: float64\n",
      "a    0.0\n",
      "b    1.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('Series的索引，默认数字索引可以工作。')\n",
    "obj = Series(np.arange(4.), index = ['a', 'b', 'c', 'd'])\n",
    "print(obj['b'])\n",
    "print(obj[3])\n",
    "print(obj[[1, 3]])\n",
    "print(obj[obj < 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series的数组切片\n",
      "b    1.0\n",
      "c    2.0\n",
      "dtype: float64\n",
      "a    0.0\n",
      "b    5.0\n",
      "c    5.0\n",
      "d    3.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('Series的数组切片')\n",
    "print(obj['b':'c'])  # 闭区间\n",
    "obj['b':'c'] = 5\n",
    "print(obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame的索引\n",
      "          one  two  three  four\n",
      "Ohio        0    1      2     3\n",
      "Colorado    4    5      6     7\n",
      "Utah        8    9     10    11\n",
      "New York   12   13     14    15\n",
      "Ohio         1\n",
      "Colorado     5\n",
      "Utah         9\n",
      "New York    13\n",
      "Name: two, dtype: int32\n",
      "          three  one\n",
      "Ohio          2    0\n",
      "Colorado      6    4\n",
      "Utah         10    8\n",
      "New York     14   12\n",
      "          one  two  three  four\n",
      "Ohio        0    1      2     3\n",
      "Colorado    4    5      6     7\n",
      "two      5\n",
      "three    6\n",
      "Name: Colorado, dtype: int32\n",
      "          four  one  two\n",
      "Colorado     7    4    5\n",
      "Utah        11    8    9\n",
      "one       8\n",
      "two       9\n",
      "three    10\n",
      "four     11\n",
      "Name: Utah, dtype: int32\n",
      "Ohio        1\n",
      "Colorado    5\n",
      "Utah        9\n",
      "Name: two, dtype: int32\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:9: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  if __name__ == '__main__':\n",
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:10: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  # Remove the CWD from sys.path while we load stuff.\n",
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:11: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  # This is added back by InteractiveShellApp.init_path()\n",
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:12: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  if sys.path[0] == '':\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('DataFrame的索引')\n",
    "data = DataFrame(np.arange(16).reshape((4, 4)),\n",
    "                  index = ['Ohio', 'Colorado', 'Utah', 'New York'],\n",
    "                  columns = ['one', 'two', 'three', 'four'])\n",
    "print(data)\n",
    "print(data['two']) # 打印列\n",
    "print(data[['three', 'one']])\n",
    "print(data[:2])\n",
    "print(data.ix['Colorado', ['two', 'three']]) # 指定索引和列\n",
    "print(data.ix[['Colorado', 'Utah'], [3, 0, 1]])\n",
    "print(data.ix[2])  # 打印第2行（从0开始）\n",
    "print(data.ix[:'Utah', 'two']) # 从开始到Utah，第2列。\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据条件选择\n",
      "          one  two  three  four\n",
      "Colorado    4    5      6     7\n",
      "Utah        8    9     10    11\n",
      "New York   12   13     14    15\n",
      "            one    two  three   four\n",
      "Ohio       True   True   True   True\n",
      "Colorado   True  False  False  False\n",
      "Utah      False  False  False  False\n",
      "New York  False  False  False  False\n",
      "          one  two  three  four\n",
      "Ohio        0    0      0     0\n",
      "Colorado    0    5      6     7\n",
      "Utah        8    9     10    11\n",
      "New York   12   13     14    15\n"
     ]
    }
   ],
   "source": [
    "print('根据条件选择')\n",
    "print(data[data.three > 5])\n",
    "print(data < 5)  # 打印True或者False\n",
    "data[data < 5] = 0\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加法\n",
      "a    7.3\n",
      "c   -2.5\n",
      "d    3.4\n",
      "e    1.5\n",
      "dtype: float64\n",
      "a   -2.1\n",
      "c    3.6\n",
      "e   -1.5\n",
      "f    4.0\n",
      "g    3.1\n",
      "dtype: float64\n",
      "a    5.2\n",
      "c    1.1\n",
      "d    NaN\n",
      "e    0.0\n",
      "f    NaN\n",
      "g    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('加法')\n",
    "s1 = Series([7.3, -2.5, 3.4, 1.5], index = ['a', 'c', 'd', 'e'])\n",
    "s2 = Series([-2.1, 3.6, -1.5, 4, 3.1], index = ['a', 'c', 'e', 'f', 'g'])\n",
    "print(s1)\n",
    "print(s2)\n",
    "print(s1 + s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame加法，索引和列都必须匹配。\n",
      "            b    c    d\n",
      "Ohio      0.0  1.0  2.0\n",
      "Texas     3.0  4.0  5.0\n",
      "Colorado  6.0  7.0  8.0\n",
      "        b   d   e\n",
      "Utah    0   1   2\n",
      "Ohio    3   4   5\n",
      "Texas   6   7   8\n",
      "Oregon  9  10  11\n",
      "            b   c     d   e\n",
      "Colorado  NaN NaN   NaN NaN\n",
      "Ohio      3.0 NaN   6.0 NaN\n",
      "Oregon    NaN NaN   NaN NaN\n",
      "Texas     9.0 NaN  12.0 NaN\n",
      "Utah      NaN NaN   NaN NaN\n"
     ]
    }
   ],
   "source": [
    "print('DataFrame加法，索引和列都必须匹配。')\n",
    "df1 = DataFrame(np.arange(9.).reshape((3, 3)),\n",
    "                columns = list('bcd'),\n",
    "                index = ['Ohio', 'Texas', 'Colorado'])\n",
    "df2 = DataFrame(np.arange(12).reshape((4, 3)),\n",
    "                columns = list('bde'),\n",
    "                index = ['Utah', 'Ohio', 'Texas', 'Oregon'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print(df1 + df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据填充\n",
      "     a    b     c     d\n",
      "0  0.0  1.0   2.0   3.0\n",
      "1  4.0  5.0   6.0   7.0\n",
      "2  8.0  9.0  10.0  11.0\n",
      "      a     b     c     d     e\n",
      "0   0.0   1.0   2.0   3.0   4.0\n",
      "1   5.0   6.0   7.0   8.0   9.0\n",
      "2  10.0  11.0  12.0  13.0  14.0\n",
      "3  15.0  16.0  17.0  18.0  19.0\n",
      "      a     b     c     d     e\n",
      "0   0.0   2.0   4.0   6.0   4.0\n",
      "1   9.0  11.0  13.0  15.0   9.0\n",
      "2  18.0  20.0  22.0  24.0  14.0\n",
      "3  15.0  16.0  17.0  18.0  19.0\n",
      "     a    b     c     d  e\n",
      "0  0.0  1.0   2.0   3.0  0\n",
      "1  4.0  5.0   6.0   7.0  0\n",
      "2  8.0  9.0  10.0  11.0  0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('数据填充')\n",
    "df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns = list('abcd'))\n",
    "df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns = list('abcde'))\n",
    "print(df1)\n",
    "print(df2)\n",
    "print(df1.add(df2, fill_value = 0))\n",
    "print(df1.reindex(columns = df2.columns, fill_value = 0))\n",
    "print\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame与Series之间的操作\n",
      "[[ 0.  1.  2.  3.]\n",
      " [ 4.  5.  6.  7.]\n",
      " [ 8.  9. 10. 11.]]\n",
      "[0. 1. 2. 3.]\n",
      "[[0. 0. 0. 0.]\n",
      " [4. 4. 4. 4.]\n",
      " [8. 8. 8. 8.]]\n",
      "        b   d   e\n",
      "Utah    0   1   2\n",
      "Ohio    3   4   5\n",
      "Texas   6   7   8\n",
      "Oregon  9  10  11\n",
      "b    0\n",
      "d    1\n",
      "e    2\n",
      "Name: Utah, dtype: int32\n",
      "        b  d  e\n",
      "Utah    0  0  0\n",
      "Ohio    3  3  3\n",
      "Texas   6  6  6\n",
      "Oregon  9  9  9\n",
      "          b   d     e   f\n",
      "Utah    0.0 NaN   3.0 NaN\n",
      "Ohio    3.0 NaN   6.0 NaN\n",
      "Texas   6.0 NaN   9.0 NaN\n",
      "Oregon  9.0 NaN  12.0 NaN\n",
      "        b  d  e\n",
      "Utah   -1  0  1\n",
      "Ohio   -1  0  1\n",
      "Texas  -1  0  1\n",
      "Oregon -1  0  1\n"
     ]
    }
   ],
   "source": [
    "print('DataFrame与Series之间的操作')\n",
    "arr = np.arange(12.).reshape((3, 4))\n",
    "print(arr)\n",
    "print(arr[0])\n",
    "print(arr - arr[0])\n",
    "frame = DataFrame(np.arange(12).reshape((4, 3)),\n",
    "                  columns = list('bde'),\n",
    "                  index = ['Utah', 'Ohio', 'Texas', 'Oregon'])\n",
    "series = frame.iloc[0]\n",
    "print(frame)\n",
    "print(series)\n",
    "print(frame - series)\n",
    "series2 = Series(range(3), index = list('bef'))\n",
    "print(frame + series2)\n",
    "series3 = frame['d']\n",
    "print(frame.sub(series3, axis = 0))  # 按列减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "函数\n",
      "               b         d         e\n",
      "Utah    0.569008 -0.877867 -1.425930\n",
      "Ohio   -0.193696 -0.279056  0.063791\n",
      "Texas  -1.910605 -0.916338  0.831096\n",
      "Oregon  0.635089  0.405282 -0.771871\n",
      "               b         d         e\n",
      "Utah    0.569008  0.877867  1.425930\n",
      "Ohio    0.193696  0.279056  0.063791\n",
      "Texas   1.910605  0.916338  0.831096\n",
      "Oregon  0.635089  0.405282  0.771871\n"
     ]
    }
   ],
   "source": [
    "print('函数')\n",
    "frame = DataFrame(np.random.randn(4, 3),\n",
    "                  columns = list('bde'),\n",
    "                  index = ['Utah', 'Ohio', 'Texas', 'Oregon'])\n",
    "print(frame)\n",
    "print(np.abs(frame))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lambda以及应用\n",
      "b    2.545693\n",
      "d    1.321620\n",
      "e    2.257026\n",
      "dtype: float64\n",
      "Utah      1.994938\n",
      "Ohio      0.342846\n",
      "Texas     2.741701\n",
      "Oregon    1.406959\n",
      "dtype: float64\n",
      "            b         d         e\n",
      "min -1.910605 -0.916338 -1.425930\n",
      "max  0.635089  0.405282  0.831096\n"
     ]
    }
   ],
   "source": [
    "print('lambda以及应用')\n",
    "f = lambda x: x.max() - x.min()\n",
    "print(frame.apply(f))\n",
    "print(frame.apply(f, axis = 1))\n",
    "def f(x):\n",
    "    return Series([x.min(), x.max()], index = ['min', 'max'])\n",
    "print(frame.apply(f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "applymap和map\n",
      "            b      d      e\n",
      "Utah     0.57  -0.88  -1.43\n",
      "Ohio    -0.19  -0.28   0.06\n",
      "Texas   -1.91  -0.92   0.83\n",
      "Oregon   0.64   0.41  -0.77\n",
      "Utah      -1.43\n",
      "Ohio       0.06\n",
      "Texas      0.83\n",
      "Oregon    -0.77\n",
      "Name: e, dtype: object\n"
     ]
    }
   ],
   "source": [
    "print('applymap和map')\n",
    "_format = lambda x: '%.2f' % x\n",
    "print(frame.applymap(_format))\n",
    "print(frame['e'].map(_format))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据索引排序，对于DataFrame可以指定轴。\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    0\n",
      "dtype: int64\n",
      "       d  a  b  c\n",
      "one    4  5  6  7\n",
      "three  0  1  2  3\n",
      "       a  b  c  d\n",
      "three  1  2  3  0\n",
      "one    5  6  7  4\n",
      "       d  c  b  a\n",
      "three  0  3  2  1\n",
      "one    4  7  6  5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('根据索引排序，对于DataFrame可以指定轴。')\n",
    "obj = Series(range(4), index = ['d', 'a', 'b', 'c'])\n",
    "print(obj.sort_index())\n",
    "frame = DataFrame(np.arange(8).reshape((2, 4)),\n",
    "                  index = ['three', 'one'],\n",
    "                  columns = list('dabc'))\n",
    "print(frame.sort_index())\n",
    "print(frame.sort_index(axis = 1))\n",
    "print(frame.sort_index(axis = 1, ascending = False)) # 降序\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据值排序\n",
      "2   -3\n",
      "3    2\n",
      "0    4\n",
      "1    7\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('根据值排序')\n",
    "obj = Series([4, 7, -3, 2])\n",
    "print(obj.sort_values()) # order已淘汰\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame指定列排序\n",
      "   b  a\n",
      "0  4  0\n",
      "1  7  1\n",
      "2 -3  0\n",
      "3  2  1\n",
      "   b  a\n",
      "2 -3  0\n",
      "3  2  1\n",
      "0  4  0\n",
      "1  7  1\n",
      "   b  a\n",
      "2 -3  0\n",
      "0  4  0\n",
      "3  2  1\n",
      "1  7  1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('DataFrame指定列排序')\n",
    "frame = DataFrame({'b':[4, 7, -3, 2], 'a':[0, 1, 0, 1]})\n",
    "print(frame)\n",
    "print(frame.sort_values(by = 'b')) # sort_index(by = ...)已淘汰\n",
    "print(frame.sort_values(by = ['a', 'b']))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rank，求排名的平均位置(从1开始)\n",
      "0    6.5\n",
      "1    1.0\n",
      "2    6.5\n",
      "3    4.5\n",
      "4    3.0\n",
      "5    2.0\n",
      "6    4.5\n",
      "dtype: float64\n",
      "0    6.0\n",
      "1    1.0\n",
      "2    7.0\n",
      "3    4.0\n",
      "4    3.0\n",
      "5    2.0\n",
      "6    5.0\n",
      "dtype: float64\n",
      "0    2.0\n",
      "1    7.0\n",
      "2    2.0\n",
      "3    4.0\n",
      "4    5.0\n",
      "5    6.0\n",
      "6    4.0\n",
      "dtype: float64\n",
      "     b  a    c\n",
      "0  4.3  0 -2.0\n",
      "1  7.0  1  5.0\n",
      "2 -3.0  0  8.0\n",
      "3  2.0  1 -2.5\n",
      "     b    a    c\n",
      "0  3.0  2.0  1.0\n",
      "1  3.0  1.0  2.0\n",
      "2  1.0  2.0  3.0\n",
      "3  3.0  2.0  1.0\n"
     ]
    }
   ],
   "source": [
    "print('rank，求排名的平均位置(从1开始)')\n",
    "obj = Series([7, -5, 7, 4, 2, 0, 4])\n",
    "# 对应排名：-5(1), 0(2), 2(3), 4(4), 4(5), 7(6), 7(7)\n",
    "print(obj.rank())\n",
    "print(obj.rank(method = 'first'))  # 去第一次出现，不求平均值。\n",
    "print(obj.rank(ascending = False, method = 'max')) # 逆序，并取最大值。所以-5的rank是7.\n",
    "frame = DataFrame({'b':[4.3, 7, -3, 2],\n",
    "                  'a':[0, 1, 0, 1],\n",
    "                  'c':[-2, 5, 8, -2.5]})\n",
    "print(frame)\n",
    "print(frame.rank(axis = 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重复的索引\n",
      "False\n",
      "          0         1         2\n",
      "a  0.304858 -0.378341  0.331010\n",
      "a  1.305813 -0.105588 -0.684957\n",
      "b  0.603363 -0.612930 -0.902760\n",
      "b -1.914711  0.612211 -0.892820\n",
      "0    0.603363\n",
      "1   -0.612930\n",
      "2   -0.902760\n",
      "Name: b, dtype: float64\n",
      "0   -1.914711\n",
      "1    0.612211\n",
      "2   -0.892820\n",
      "Name: b, dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  \n",
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:9: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "print('重复的索引')\n",
    "obj = Series(range(5), index = ['a', 'a', 'b', 'b', 'c'])\n",
    "print(obj.index.is_unique) # 判断是非有重复索引\n",
    "#print(obj['a'][0])\n",
    "#print(obj.a[1])\n",
    "df = DataFrame(np.random.randn(4, 3), index = ['a', 'a', 'b', 'b'])\n",
    "print(df)\n",
    "print(df.ix['b'].ix[0])\n",
    "print(df.ix['b'].ix[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "求和\n",
      "    one  two\n",
      "a  1.40  NaN\n",
      "b  7.10 -4.5\n",
      "c   NaN  NaN\n",
      "d  0.75 -1.3\n",
      "one    9.25\n",
      "two   -5.80\n",
      "dtype: float64\n",
      "a    1.40\n",
      "b    2.60\n",
      "c    0.00\n",
      "d   -0.55\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('求和')\n",
    "df = DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],\n",
    "              index = ['a', 'b', 'c', 'd'],\n",
    "              columns = ['one', 'two'])\n",
    "print(df)\n",
    "print(df.sum())  # 按列求和\n",
    "print(df.sum(axis = 1))  # 按行求和\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均数\n",
      "a      NaN\n",
      "b    1.300\n",
      "c      NaN\n",
      "d   -0.275\n",
      "dtype: float64\n",
      "a    1.400\n",
      "b    1.300\n",
      "c      NaN\n",
      "d   -0.275\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('平均数')\n",
    "print(df.mean(axis = 1, skipna = False))\n",
    "print(df.mean(axis = 1))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "其它\n",
      "one    b\n",
      "two    d\n",
      "dtype: object\n",
      "    one  two\n",
      "a  1.40  NaN\n",
      "b  8.50 -4.5\n",
      "c   NaN  NaN\n",
      "d  9.25 -5.8\n",
      "            one       two\n",
      "count  3.000000  2.000000\n",
      "mean   3.083333 -2.900000\n",
      "std    3.493685  2.262742\n",
      "min    0.750000 -4.500000\n",
      "25%    1.075000 -3.700000\n",
      "50%    1.400000 -2.900000\n",
      "75%    4.250000 -2.100000\n",
      "max    7.100000 -1.300000\n",
      "count     16\n",
      "unique     3\n",
      "top        a\n",
      "freq       8\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print('其它')\n",
    "print(df.idxmax())\n",
    "print(df.cumsum())\n",
    "print(df.describe())\n",
    "obj = Series(['a', 'a', 'b', 'c'] * 4)\n",
    "print(obj.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重\n",
      "['c' 'a' 'd' 'b']\n",
      "c    3\n",
      "a    3\n",
      "b    2\n",
      "d    1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print('去重')\n",
    "obj = Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])\n",
    "print(obj.unique())\n",
    "print(obj.value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "判断元素存在\n",
      "0     True\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5     True\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "dtype: bool\n",
      "0    c\n",
      "5    b\n",
      "6    b\n",
      "7    c\n",
      "8    c\n",
      "dtype: object\n",
      "   Qu1  Qu2  Qu3\n",
      "0    1    2    1\n",
      "1    3    3    5\n",
      "2    4    1    2\n",
      "3    3    2    4\n",
      "4    4    3    4\n",
      "   Qu1  Qu2  Qu3\n",
      "1  1.0  1.0  1.0\n",
      "2  0.0  2.0  1.0\n",
      "3  2.0  2.0  0.0\n",
      "4  2.0  0.0  2.0\n",
      "5  0.0  0.0  1.0\n",
      "     1    2    3    4    5\n",
      "0  2.0  1.0  0.0  0.0  0.0\n",
      "1  0.0  0.0  2.0  0.0  1.0\n",
      "2  1.0  1.0  0.0  1.0  0.0\n",
      "3  0.0  1.0  1.0  1.0  0.0\n",
      "4  0.0  0.0  1.0  2.0  0.0\n"
     ]
    }
   ],
   "source": [
    "print('判断元素存在')\n",
    "mask = obj.isin(['b', 'c'])\n",
    "print(mask)\n",
    "print(obj[mask]) #只打印元素b和c\n",
    "data = DataFrame({'Qu1':[1, 3, 4, 3, 4],\n",
    "                  'Qu2':[2, 3, 1, 2, 3],\n",
    "                  'Qu3':[1, 5, 2, 4, 4]})\n",
    "print(data)\n",
    "print(data.apply(pd.value_counts).fillna(0))\n",
    "print(data.apply(pd.value_counts, axis = 1).fillna(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "作为null处理的值\n",
      "0     aardvark\n",
      "1    artichoke\n",
      "2          NaN\n",
      "3      avocado\n",
      "dtype: object\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3    False\n",
      "dtype: bool\n",
      "0     True\n",
      "1    False\n",
      "2     True\n",
      "3    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print('作为null处理的值')\n",
    "string_data = Series(['aardvark', 'artichoke', np.nan, 'avocado'])\n",
    "print(string_data)\n",
    "print(string_data.isnull())\n",
    "string_data[0] = None\n",
    "print(string_data.isnull())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "丢弃NA\n",
      "0    1.0\n",
      "2    3.5\n",
      "4    7.0\n",
      "dtype: float64\n",
      "0    1.0\n",
      "2    3.5\n",
      "4    7.0\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('丢弃NA')\n",
    "data = Series([1, np.nan, 3.5, np.nan, 7])\n",
    "print(data.dropna())\n",
    "print(data[data.notnull()])\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame对丢弃NA的处理\n",
      "     0    1    2\n",
      "0  1.0  6.5  3.0\n",
      "     0    1    2\n",
      "0  1.0  6.5  3.0\n",
      "1  1.0  NaN  NaN\n",
      "3  NaN  6.5  3.0\n",
      "     0    1    2\n",
      "0  1.0  6.5  3.0\n",
      "1  1.0  NaN  NaN\n",
      "2  NaN  NaN  NaN\n",
      "3  NaN  6.5  3.0\n",
      "          0         1         2\n",
      "0  0.729572       NaN       NaN\n",
      "1  0.042038       NaN       NaN\n",
      "2  0.621917       NaN -0.895003\n",
      "3  0.764016       NaN -0.498367\n",
      "4  1.440623 -0.523136 -0.688565\n",
      "5  0.545126  0.257568 -0.381991\n",
      "6  0.386460 -0.084571  1.244007\n",
      "          0         1         2\n",
      "2  0.621917       NaN -0.895003\n",
      "3  0.764016       NaN -0.498367\n",
      "4  1.440623 -0.523136 -0.688565\n",
      "5  0.545126  0.257568 -0.381991\n",
      "6  0.386460 -0.084571  1.244007\n"
     ]
    }
   ],
   "source": [
    "print('DataFrame对丢弃NA的处理')\n",
    "data = DataFrame([[1., 6.5, 3.], [1., np.nan, np.nan],\n",
    "                  [np.nan, np.nan, np.nan], [np.nan, 6.5, 3.]])\n",
    "print(data.dropna()) # 默认只要某行有NA就全部删除\n",
    "print(data.dropna(how = 'all'))  # 全部为NA才删除\n",
    "data[4] = np.nan  # 新增一列\n",
    "print(data.dropna(axis = 1, how = 'all'))\n",
    "data = DataFrame(np.random.randn(7, 3))\n",
    "data.iloc[:4, 1] = np.nan\n",
    "data.iloc[:2, 2] = np.nan\n",
    "print(data)\n",
    "print(data.dropna(thresh = 2)) # 每行至少要有2个非NA元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "填充0\n",
      "          0         1         2\n",
      "0  0.026179  0.000000  0.000000\n",
      "1  0.451428  0.000000  0.000000\n",
      "2  2.084856  0.000000 -1.378586\n",
      "3  0.503450  0.000000 -1.302039\n",
      "4  0.827106 -0.076039  0.672745\n",
      "5  0.067026  2.691673 -2.247898\n",
      "6  1.775927  0.806902 -0.016085\n",
      "          0         1         2\n",
      "0  0.026179  0.000000  0.000000\n",
      "1  0.451428  0.000000  0.000000\n",
      "2  2.084856  0.000000 -1.378586\n",
      "3  0.503450  0.000000 -1.302039\n",
      "4  0.827106 -0.076039  0.672745\n",
      "5  0.067026  2.691673 -2.247898\n",
      "6  1.775927  0.806902 -0.016085\n"
     ]
    }
   ],
   "source": [
    "print('填充0')\n",
    "df = DataFrame(np.random.randn(7, 3))\n",
    "df.iloc[:4, 1] = np.nan\n",
    "df.iloc[:2, 2] = np.nan\n",
    "print(df.fillna(0))\n",
    "df.fillna(0, inplace = True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同行列填充不同的值\n",
      "          0         1         2\n",
      "0  0.026179  0.000000  0.000000\n",
      "1  0.451428  0.000000  0.000000\n",
      "2  2.084856  0.000000 -1.378586\n",
      "3  0.503450  0.000000 -1.302039\n",
      "4  0.827106 -0.076039  0.672745\n",
      "5  0.067026  2.691673 -2.247898\n",
      "6  1.775927  0.806902 -0.016085\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('不同行列填充不同的值')\n",
    "print(df.fillna({1:0.5, 3:-1}))  # 第3列不存在\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同的填充方式\n",
      "          0         1         2\n",
      "0  0.809324 -1.336091 -1.101112\n",
      "1  0.823888 -0.423533 -0.141792\n",
      "2 -0.800332       NaN  1.331387\n",
      "3 -1.191404       NaN -1.136156\n",
      "4 -0.514338       NaN       NaN\n",
      "5 -2.530825       NaN       NaN\n",
      "          0         1         2\n",
      "0  0.809324 -1.336091 -1.101112\n",
      "1  0.823888 -0.423533 -0.141792\n",
      "2 -0.800332 -0.423533  1.331387\n",
      "3 -1.191404 -0.423533 -1.136156\n",
      "4 -0.514338 -0.423533 -1.136156\n",
      "5 -2.530825 -0.423533 -1.136156\n",
      "          0         1         2\n",
      "0  0.809324 -1.336091 -1.101112\n",
      "1  0.823888 -0.423533 -0.141792\n",
      "2 -0.800332 -0.423533  1.331387\n",
      "3 -1.191404 -0.423533 -1.136156\n",
      "4 -0.514338       NaN -1.136156\n",
      "5 -2.530825       NaN -1.136156\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('不同的填充方式')\n",
    "df = DataFrame(np.random.randn(6, 3))\n",
    "df.iloc[2:, 1] = np.nan\n",
    "df.iloc[4:, 2] = np.nan\n",
    "print(df)\n",
    "print(df.fillna(method = 'ffill'))\n",
    "print(df.fillna(method = 'ffill', limit = 2))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用统计数据填充\n",
      "0    1.000000\n",
      "1    3.833333\n",
      "2    3.500000\n",
      "3    3.833333\n",
      "4    7.000000\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('用统计数据填充')\n",
    "data = Series([1., np.nan, 3.5, np.nan, 7])\n",
    "print(data.fillna(data.mean()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series的层次索引\n",
      "a  1   -0.840808\n",
      "   2    0.577752\n",
      "   3   -0.467863\n",
      "b  1   -1.112062\n",
      "   2   -2.532389\n",
      "   3    1.822776\n",
      "c  1    1.319434\n",
      "   2   -0.759804\n",
      "d  2    1.537555\n",
      "   3   -0.430978\n",
      "dtype: float64\n",
      "MultiIndex(levels=[['a', 'b', 'c', 'd'], [1, 2, 3]],\n",
      "           codes=[[0, 0, 0, 1, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 1, 2]])\n",
      "1   -1.112062\n",
      "2   -2.532389\n",
      "3    1.822776\n",
      "dtype: float64\n",
      "b  1   -1.112062\n",
      "   2   -2.532389\n",
      "   3    1.822776\n",
      "c  1    1.319434\n",
      "   2   -0.759804\n",
      "dtype: float64\n",
      "a  1   -0.840808\n",
      "   2    0.577752\n",
      "dtype: float64\n",
      "          1         2         3\n",
      "a -0.840808  0.577752 -0.467863\n",
      "b -1.112062 -2.532389  1.822776\n",
      "c  1.319434 -0.759804       NaN\n",
      "d       NaN  1.537555 -0.430978\n",
      "a  1   -0.840808\n",
      "   2    0.577752\n",
      "   3   -0.467863\n",
      "b  1   -1.112062\n",
      "   2   -2.532389\n",
      "   3    1.822776\n",
      "c  1    1.319434\n",
      "   2   -0.759804\n",
      "d  2    1.537555\n",
      "   3   -0.430978\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Series的层次索引')\n",
    "data = Series(np.random.randn(10),\n",
    "              index = [['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],\n",
    "                       [1, 2, 3, 1, 2, 3, 1, 2, 2, 3]])\n",
    "print(data)\n",
    "print(data.index)\n",
    "print(data.b)\n",
    "print(data['b':'c'])\n",
    "print(data[:2])\n",
    "print(data.unstack())\n",
    "print(data.unstack().stack())\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame的层次索引\n",
      "     Ohio     Colorado\n",
      "    Green Red    Green\n",
      "a 1     0   1        2\n",
      "  2     3   4        5\n",
      "b 1     6   7        8\n",
      "  2     9  10       11\n",
      "state      Ohio     Colorado\n",
      "color     Green Red    Green\n",
      "key1 key2                   \n",
      "a    1        0   1        2\n",
      "     2        3   4        5\n",
      "b    1        6   7        8\n",
      "     2        9  10       11\n",
      "state     color\n",
      "Ohio      Green    0\n",
      "          Red      1\n",
      "Colorado  Green    2\n",
      "Name: (a, 1), dtype: int32\n",
      "color\n",
      "Green    5\n",
      "Name: (a, 2), dtype: int32\n",
      "4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('DataFrame的层次索引')\n",
    "frame = DataFrame(np.arange(12).reshape((4, 3)),\n",
    "                  index = [['a', 'a', 'b', 'b'], [1, 2, 1, 2]],\n",
    "                  columns = [['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']])\n",
    "print(frame)\n",
    "frame.index.names = ['key1', 'key2']\n",
    "frame.columns.names = ['state', 'color']\n",
    "print(frame)\n",
    "print(frame.loc['a', 1])\n",
    "print(frame.loc['a', 2]['Colorado'])\n",
    "print(frame.loc['a', 2]['Ohio']['Red'])\n",
    "print\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "直接用MultiIndex创建层次索引结构\n",
      "MultiIndex(levels=[['Colorado', 'Ohio'], ['Gree', 'Green', 'Red']],\n",
      "           codes=[[1, 1, 0], [0, 2, 1]],\n",
      "           names=['state', 'color'])\n"
     ]
    }
   ],
   "source": [
    "print('直接用MultiIndex创建层次索引结构')\n",
    "print(MultiIndex.from_arrays([['Ohio', 'Ohio', 'Colorado'], ['Gree', 'Red', 'Green']],\n",
    "                             names = ['state', 'color'])\n",
    "                             )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引层级交换\n",
      "           Ohio     Colorado\n",
      "          Green Red    Green\n",
      "key2 key1                   \n",
      "1    a        0   1        2\n",
      "2    a        3   4        5\n",
      "1    b        6   7        8\n",
      "2    b        9  10       11\n",
      "           Ohio     Colorado\n",
      "          Green Red    Green\n",
      "key1 key2                   \n",
      "a    1        0   1        2\n",
      "     2        3   4        5\n",
      "b    1        6   7        8\n",
      "     2        9  10       11\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('索引层级交换')\n",
    "frame = DataFrame(np.arange(12).reshape((4, 3)),\n",
    "                  index = [['a', 'a', 'b', 'b'], [1, 2, 1, 2]],\n",
    "                  columns = [['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']])\n",
    "frame.index.names = ['key1', 'key2']\n",
    "frame_swapped = frame.swaplevel('key1', 'key2')\n",
    "print(frame_swapped)\n",
    "print(frame_swapped.swaplevel(0, 1))\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据指定的key计算统计信息\n",
      "           Ohio     Colorado\n",
      "          Green Red    Green\n",
      "key1 key2                   \n",
      "a    1        0   1        2\n",
      "     2        3   4        5\n",
      "b    1        6   7        8\n",
      "     2        9  10       11\n",
      "      Ohio     Colorado\n",
      "     Green Red    Green\n",
      "key2                   \n",
      "1        6   8       10\n",
      "2       12  14       16\n"
     ]
    }
   ],
   "source": [
    "print('根据指定的key计算统计信息')\n",
    "frame = DataFrame(np.arange(12).reshape((4, 3)),\n",
    "                  index = [['a', 'a', 'b', 'b'], [1, 2, 1, 2]],\n",
    "                  columns = [['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']])\n",
    "frame.index.names = ['key1', 'key2']\n",
    "print(frame)\n",
    "print(frame.sum(level = 'key2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用列生成层次索引\n",
      "   a  b    c  d\n",
      "0  0  7  one  0\n",
      "1  1  6  one  1\n",
      "2  2  5  one  2\n",
      "3  3  4  two  0\n",
      "4  4  3  two  1\n",
      "5  5  2  two  2\n",
      "6  6  1  two  3\n",
      "       a  b\n",
      "c   d      \n",
      "one 0  0  7\n",
      "    1  1  6\n",
      "    2  2  5\n",
      "two 0  3  4\n",
      "    1  4  3\n",
      "    2  5  2\n",
      "    3  6  1\n",
      "       a  b    c  d\n",
      "c   d              \n",
      "one 0  0  7  one  0\n",
      "    1  1  6  one  1\n",
      "    2  2  5  one  2\n",
      "two 0  3  4  two  0\n",
      "    1  4  3  two  1\n",
      "    2  5  2  two  2\n",
      "    3  6  1  two  3\n",
      "     c  d  a  b\n",
      "0  one  0  0  7\n",
      "1  one  1  1  6\n",
      "2  one  2  2  5\n",
      "3  two  0  3  4\n",
      "4  two  1  4  3\n",
      "5  two  2  5  2\n",
      "6  two  3  6  1\n"
     ]
    }
   ],
   "source": [
    "print('使用列生成层次索引')\n",
    "frame = DataFrame({'a':range(7),\n",
    "                   'b':range(7, 0, -1),\n",
    "                   'c':['one', 'one', 'one', 'two', 'two', 'two', 'two'],\n",
    "                   'd':[0, 1, 2, 0, 1, 2, 3]})\n",
    "print(frame)\n",
    "print(frame.set_index(['c', 'd']))  # 把c/d列变成索引\n",
    "print(frame.set_index(['c', 'd'], drop = False)) # 列依然保留\n",
    "frame2 = frame.set_index(['c', 'd'])\n",
    "print(frame2.reset_index())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "整数索引\n",
      "0    0.0\n",
      "1    1.0\n",
      "2    2.0\n",
      "dtype: float64\n",
      "<class 'KeyError'>\n",
      "2.0\n",
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function print>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('整数索引')\n",
    "ser = Series(np.arange(3.))\n",
    "print(ser)\n",
    "try:\n",
    "    print(ser[-1]) # 这里会有歧义\n",
    "except:\n",
    "    print(sys.exc_info()[0])\n",
    "ser2 = Series(np.arange(3.), index = ['a', 'b', 'c'])\n",
    "print(ser2[-1])\n",
    "ser3 = Series(range(3), index = [-5, 1, 3])\n",
    "print(ser3.iloc[2])  # 避免直接用[2]产生的歧义\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对DataFrame使用整数索引\n",
      "   0  1\n",
      "2  0  1\n",
      "0  2  3\n",
      "1  4  5\n",
      "0    0\n",
      "1    1\n",
      "Name: 2, dtype: int32\n",
      "2    1\n",
      "0    3\n",
      "1    5\n",
      "Name: 1, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print('对DataFrame使用整数索引')\n",
    "frame = DataFrame(np.arange(6).reshape((3, 2)), index = [2, 0, 1])\n",
    "print(frame)\n",
    "print(frame.iloc[0])\n",
    "print(frame.iloc[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.panel.Panel'>\n",
      "Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)\n",
      "Items axis: 0 to 1\n",
      "Major_axis axis: 0 to 3\n",
      "Minor_axis axis: 0 to 4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Program Files\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3296: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    }
   ],
   "source": [
    "data = np.random.rand(2,4,5)\n",
    "p = pd.Panel(data)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
